package com.itheima.web;

import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandServlet {
    @Autowired
    private BrandService brandService;

    //添加
    @PostMapping("/add")
    public String add(@RequestBody Brand brand) {
        try {
            brandService.add(brand);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("添加失败");
            return "false";
        }
        return "true";
    }

    //批量删除
    @PostMapping("/deleteByIds")
    public String deleteByIds(@RequestBody List<Integer> ids) {
        try {
            brandService.deleteByIds(ids);
            System.out.println("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("删除失败");
            return "false";
        }
        return "true";
    }

    //根据id删除
    @DeleteMapping("/deleteById")
    public String deleteById(int id) {
        try {
            brandService.deleteById(id);
            System.out.println("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("删除失败");
            return "false";
        }
        return "true";
    }

    //修改
    @PutMapping("/update")
    public String update(@RequestBody Brand brand) {
        try {
            brandService.update(brand);
            System.out.println("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("修改失败");
            return "false";
        }
        return "true";
    }

    /**
     * 条件查询
     */
    @PostMapping("/selectByPageAndCondition/{currentPage}&{pageSize}")
    public PageBean<Brand> selectByPageAndCondition(@PathVariable int currentPage, @PathVariable int pageSize, @RequestBody Brand brand) {
        PageBean<Brand> pageBean = brandService.selectByPageAndCondition(brand, currentPage, pageSize);
        System.out.println("正在查询....");
        return pageBean;
    }

    @GetMapping("/findById")
    public Brand findById(int id) {
        return brandService.findById(id);
    }
}
